<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// get and set default controller, task
$task = JRequest::getWord('task', 'display');
echo 'a';
switch($task){
    case 'actt':
    
    break;
    case 'actt':
    
    break;
    case 'actt':
    
    break;
}

/**
* Dong bo data from mysql (table OCBA) to file csv
* Author: Hung
*/
function dongbo_csv_ocba(){
    $csv_output  = "拠点タイプ,センタークラブコード,所属センターコード,名称,公式HP URL,画像,説明,保有施設,郵便番号,都道府県,住所1,住所2,TEL,FAX,ご利用時間,休館日,開館/閉館時間,名称,郵便番号,住所1,住所2,TEL,FAX,市区町村,町の紹介,宣言,人口,世帯数,面積,人口密度,主要産業,都市のシンボル（鳥）,都市のシンボル（花）,都市のシンボル（木）,都市のシンボル（その他）,姉妹交流・交流都市（国内）,姉妹交流・交流都市（国外）,役場 郵便番号,役場 都道府県,役場 住所1,役場 住所2,役場 TEL,役場 FAX,最寄駅,備考";
    $csv_output  = mb_convert_encoding($csv_output,'Shift-JIS','UTF-8'); 
    $csv_output .= "\n";
    $qr = "SELECT OCBA_CODE,OCBA_CODE as code,OCBA_CLUB_DEPEND,OCBA_NAME,OCBA_URL,OCBA_IMG,
            OCBA_PR,GYMT_CODE,POOL_POLK,POOL_POHK,DOCK_CODE,OCBA_POST,PREF_NAME,OCBA_ADD1,
            OCBA_ADD2,OCBA_TEL,OCBA_LFAX,OCBA_STIME,OCBA_REST,OCBA_PERI,OCBA_LNAME,OCBA_LPOST,
            OCBA_LADD1,OCBA_LADD2,OCBA_LTEL,OCBA_LFAX,
            TOWN_NAME,TOWN_PR,TOWN_DECL,TOWN_POPU,TOWN_HOUS,TOWN_AREA,TOWN_DENS,
            TOWN_INDU,TOWN_SYM1,TOWN_SYM2,TOWN_SYM3,TOWN_SYM4,TOWN_SIS1,TOWN_SIS2,
            TOWN_POST,PREF_NAME,TOWN_ADD1,TOWN_ADD2,TOWN_TEL,TOWN_FAX,TOWN_STAT,TOWN_NOTE
            FROM ocba o
            LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
            LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
            LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
            INNER JOIN town ON OCBA_TOWN_CODE = TOWN_CODE
            INNER JOIN pref ON TOWN_PREF_CODE = PREF_CODE
            WHERE OCBA_WEBF = 1
            ORDER BY o.OCBA_CODE DESC";

    $db = JFactory::getDbo();
	$db->setQuery($qr);
    $result = $db->loadObjectList();
    
    if ($result != null){
        foreach ($result as $item) {
            substr($item->OCBA_CODE,0,1) == '0' ? $item->OCBA_CODE = "センター" : $item->OCBA_CODE = "クラブ";
            $item->GYMT_CODE != null ? $item->GYMT_CODE = "体育館," : $item->GYMT_CODE;
            if($item->POOL_POLK == 3){
                $item->POOL_POLK = "プール（屋内）,";
            }
            if($item->POOL_POHK == 2){
                $item->POOL_POHK = "プール〔加温）,";
            }
            if($item->POOL_POHK == 3){
                $item->POOL_POHK = "プール（温水なし）,";
            }
            $item->DOCK_CODE != null ? $item->DOCK_CODE = "艇庫" : $item->DOCK_CODE;
            $item->GYMT_CODE = $item->GYMT_CODE.$item->POOL_POLK.$item->POOL_POHK.$item->DOCK_CODE;
        	foreach ($item as $key => $value) {
        	   if($key != "POOL_POLK" && $key != "POOL_POHK" && $key != "DOCK_CODE"){
        	       if($key == "TOWN_NOTE"){
        	           $csv_output .= "\"".mb_convert_encoding($value,'Shift-JIS','UTF-8')."\""; 
            	   }else{
            		  $csv_output .= "\"".mb_convert_encoding($value,'Shift-JIS','UTF-8')."\",";
                    }
        	   }
        	}
        	$csv_output .= "\r\n";
            $db->setQuery("UPDATE ocba SET OCBA_DBSY = 1 WHERE OCBA_CODE = '$item->code'");
            $db->query();
        }
    }
    
    $filename = "db_OCBA".date("Y-m-d_H-i",time());

    header("Content-Encoding: Shift-JIS");
    header("Content-Type: text/csv; charset= Shift-JIS");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition:attachment; filename=" . $filename . ".csv");
    print_r($csv_output);
    exit;
}


/**
* Dong bo data from mysql (table TOWN) to file csv
* Author: Hung
*/
function dongbo_csv_town(){
    $csv_output  = "拠点タイプ,センタークラブコード,所属センターコード,名称,公式HP URL,画像,説明,保有施設,郵便番号,都道府県,住所1,住所2,TEL,FAX,ご利用時間,休館日,開館期間,名称,郵便番号,住所1,住所2,TEL,FAX,市区町村,町の紹介,宣言,人口,世帯数,面積,人口密度,主要産業,都市のシンボル（鳥）,都市のシンボル（花）,都市のシンボル（木）,都市のシンボル（その他）,姉妹交流・交流都市（国内）,姉妹交流・交流都市（国外）,役場 郵便番号,役場 都道府県,役場 住所1,役場 住所2,役場 TEL,役場 FAX,最寄駅,備考";
    $csv_output  = mb_convert_encoding($csv_output,'Shift-JIS','UTF-8'); 
    $csv_output .= "\n";
    $qr = "SELECT OCBA_CODE,TOWN_CODE,OCBA_CODE as code,OCBA_CLUB_DEPEND,OCBA_NAME,OCBA_URL,OCBA_IMG,
            OCBA_PR,GYMT_CODE,POOL_POLK,POOL_POHK,DOCK_CODE,OCBA_POST,PREF_NAME,OCBA_ADD1,
            OCBA_ADD2,OCBA_TEL,OCBA_LFAX,OCBA_STIME,OCBA_ETIME,OCBA_REST,OCBA_PERI,OCBA_LNAME,OCBA_LPOST,
            OCBA_LADD1,OCBA_LADD2,OCBA_LTEL,OCBA_LFAX,
            TOWN_NAME,TOWN_PR,TOWN_DECL,TOWN_POPU,TOWN_HOUS,TOWN_AREA,TOWN_DENS,
            TOWN_INDU,TOWN_SYM1,TOWN_SYM2,TOWN_SYM3,TOWN_SYM4,TOWN_SIS1,TOWN_SIS2,
            TOWN_POST,PREF_NAME,TOWN_ADD1,TOWN_ADD2,TOWN_TEL,TOWN_FAX,TOWN_STAT,TOWN_NOTE
            FROM  town
            LEFT JOIN ocba o ON TOWN_CODE = OCBA_TOWN_CODE
            LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
            LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
            LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
            INNER JOIN pref ON TOWN_PREF_CODE = PREF_CODE
            WHERE TOWN_DBSY = 0 AND TOWN_WEBF = 1 AND TOWN_FLAG = 1
            ORDER BY o.OCBA_CODE DESC";

    $db = JFactory::getDbo();
	$db->setQuery($qr);
    $result = $db->loadObjectList();
    
    if ($result != null){
        foreach ($result as $item) {
            substr($item->OCBA_CODE,0,1) == '0' ? $item->OCBA_CODE = "センター" : $item->OCBA_CODE = "クラブ";
            $item->GYMT_CODE != null ? $item->GYMT_CODE = "体育館," : $item->GYMT_CODE;
            if($item->POOL_POLK == 3){
                $item->POOL_POLK = "プール（屋内）,";
            }
            else{
                $item->POOL_POLK = "";
            }
            if($item->POOL_POHK == 2){
                $item->POOL_POHK = "プール〔加温）,";
            }
            elseif($item->POOL_POHK == 3){
                $item->POOL_POHK = "プール（温水なし）,";
            }
            else{
                $item->POOL_POHK = "";
            }
            $item->DOCK_CODE != null ? $item->DOCK_CODE = "艇庫" : $item->DOCK_CODE;
            $item->GYMT_CODE = $item->GYMT_CODE.$item->POOL_POLK.$item->POOL_POHK.$item->DOCK_CODE;
            $item->OCBA_STIME = $item->OCBA_STIME."-".$item->OCBA_ETIME;
        	foreach ($item as $key => $value) {
        	   if($key != "POOL_POLK" && $key != "POOL_POHK" && $key != "DOCK_CODE" && $key != "OCBA_ETIME"){
        	       if($key == "TOWN_NOTE"){
        	           $csv_output .= "\"".mb_convert_encoding($value,'Shift-JIS','UTF-8')."\""; 
            	   }else{
            		  $csv_output .= "\"".mb_convert_encoding($value,'Shift-JIS','UTF-8')."\",";
                    }
        	   }
        	}
        	$csv_output .= "\r\n";
            $db->setQuery("UPDATE town SET TOWN_DBSY = 1 WHERE TOWN_CODE = '$item->TOWN_CODE'");
            $db->query();
        }
    }
    
    $filename = "db_TOWN".date("Y-m-d_H-i",time());

    header("Content-Encoding: Shift-JIS");
    header("Content-Type: text/csv; charset= Shift-JIS");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition:attachment; filename=" . $filename . ".csv");
    print_r($csv_output);
    exit;
}

/**
* Dong bo data from mysql (table ACTT) to file csv
* Author: Hung
*/
function dongbo_csv_actt(){
    //$csv_output = "イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,イベント情報,事業概要,事業概要,事業概要,事業概要,事業概要,事業概要,事業概要,ジャンル,ジャンル,ジャンル,ファイル,ファイル,ファイル,ファイル,ファイル,ファイル,ファイル,ファイル,ファイル";
    //$csv_output .= "\n";
    $csv_output  = "イベントID,公開フラグ,募集ステータス,オススメ度,開催期間,イベントタイトル,会場,主催,後援,協力,問合わせ先,問合わせ先 担当者名,問合わせ先 電話番号,センター・クラブコード,趣旨,対象,性別,参加人数,参加費,事業内容,備考,施設,対象,内容,画像1,画像2,画像3,添付資料1,添付資料2,添付資料3,添付資料4,添付資料5,添付資料6";
    $csv_output  = mb_convert_encoding($csv_output,'Shift-JIS','UTF-8'); 
    $csv_output .= "\n";
    $qr = "SELECT ACTT_CODE,ACTT_FLAG,ACTT_RECR,ACTT_RECOMMENDED_DEGREE,ACTT_PERIOD_START,ACTT_PERIOD_FINISH,
            ACTT_NAME,ACTT_VENUE,ACTT_TOCHUC,ACTT_TAITRO,ACTT_COOPERATION,
            ACTT_CONTACT,ACTT_PERSON,ACTT_TEL,ACTT_OCBA_CODE,ACTT_EFFECT,ACTT_TARGET,
            ACTT_SEX,ACTT_NUMBER_OF_PARTICIPANTS,ACTT_ENTRY_FEE,
            ACTT_BUSINESS,ACTT_REMARKS,ACTT_MFAC_CODE_LIST,ACTT_MOBJ_CODE_LIST,ACTT_MCON_CODE_LIST,
            ACTT_IMAGE_1,ACTT_IMAGE_2,ACTT_IMAGE_3,ACTT_ATTACHMENT_1,ACTT_ATTACHMENT_2,
            ACTT_ATTACHMENT_3,ACTT_ATTACHMENT_4,ACTT_ATTACHMENT_5,ACTT_ATTACHMENT_6
            FROM actt
            WHERE ACTT_DBSY = 0 AND ACTT_WEBF = 1 AND ACTT_FLAG = 1
            ORDER BY ACTT_CODE DESC";

    $db = JFactory::getDbo();
	$db->setQuery($qr);
    $result = $db->loadObjectList();
    
    if ($result != null){
        foreach ($result as $item) {
            if($item->ACTT_RECR == 1){
                $item->ACTT_RECR = "募集開始前";
            }
            elseif(($item->ACTT_RECR == 2)){
                $item->ACTT_RECR = "募集中";
            }
            else{
                $item->ACTT_RECR = "募集終了";
            }
            if($item->ACTT_RECOMMENDED_DEGREE == 1){
                $item->ACTT_RECOMMENDED_DEGREE = "★";
            }
            elseif($item->ACTT_RECOMMENDED_DEGREE == 2){
                $item->ACTT_RECOMMENDED_DEGREE = "★★";
            }
            else{
                $item->ACTT_RECOMMENDED_DEGREE = "★★★";
            }
            $item->ACTT_PERIOD_START = HelperBG::F_GetDate_JP($item->ACTT_PERIOD_START)."/".HelperBG::F_GetDate_JP($item->ACTT_PERIOD_FINISH);
            if($item->ACTT_SEX == 0){
                $item->ACTT_SEX = "男性";
            }
            elseif($item->ACTT_SEX == 1){
                $item->ACTT_SEX = "女性";
            }
            else{
                $item->ACTT_SEX = "全て";
            }
            
            //Xu ly MFAC
            $db->setQuery("SELECT MFAC_NAME FROM mfac WHERE MFAC_CODE IN ($item->ACTT_MFAC_CODE_LIST)");
            $mfac = $db->loadObjectList();
            $temp1 = array();
            if($mfac != null){             
                foreach($mfac as $item1){
                    $temp1[] = $item1->MFAC_NAME;
                    }
            }
            $temp1 = implode(",",$temp1);
            $item->ACTT_MFAC_CODE_LIST = $temp1;
            
            //Xu ly MOBJ
            $db->setQuery("SELECT MOBJ_NAME FROM mobj WHERE MOBJ_CODE IN ($item->ACTT_MOBJ_CODE_LIST)");
            $mobj = $db->loadObjectList();
            $temp2 = array();
            if($mobj != null){             
                foreach($mobj as $item2){
                    $temp2[] = $item2->MOBJ_NAME;
                    }
            }
            $temp2 = implode(",",$temp2);
            $item->ACTT_MOBJ_CODE_LIST = $temp2;
            
            //Xu ly MCON
            $db->setQuery("SELECT MCON_NAME FROM mcon WHERE MCON_CODE IN ($item->ACTT_MCON_CODE_LIST)");
            $mcon = $db->loadObjectList();
            $temp3 = array();
            if($mcon != null){             
                foreach($mcon as $item3){
                    $temp3[] = $item3->MCON_NAME;
                    }
            }
            $temp3 = implode(",",$temp3);
            $item->ACTT_MCON_CODE_LIST = $temp3;
            
        	foreach ($item as $key => $value) {
        	   if($key != "ACTT_PERIOD_FINISH"){
        	       if($key == "ACTT_ATTACHMENT_6"){
        	           $csv_output .= "\"".mb_convert_encoding($value,'Shift-JIS','UTF-8')."\""; 
            	   }else{
            		  $csv_output .= "\"".mb_convert_encoding($value,'Shift-JIS','UTF-8')."\",";
                    }
        	   }
        	}
        	$csv_output .= "\r\n";
            $db->setQuery("UPDATE actt SET ACTT_DBSY = 1 WHERE ACTT_CODE = '$item->ACTT_CODE'");
            $db->query();
        }
    }
    
    $filename = "db_ACTT".date("Y-m-d_H-i",time());

    header("Content-Encoding: Shift-JIS");
    header("Content-Type: text/csv; charset= Shift-JIS");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition:attachment; filename=" . $filename . ".csv");
    print_r($csv_output);
    exit;
}